package com.leezt.designpattern.creation.s02Factory.basic;

import com.leezt.designpattern.creation.s02Factory.basic.service.IRuleConfigParserFactory;
import com.leezt.designpattern.creation.s02Factory.common.RuleConfig;
import com.leezt.designpattern.creation.s02Factory.common.exceptions.InvalidRuleConfigException;
import com.leezt.designpattern.creation.s02Factory.service.IRuleConfigParser;

public class RuleConfigSourceV2 {
    public RuleConfig load(String ruleConfigFilePath) throws InvalidRuleConfigException {
        String ruleConfigFileExtension = getFileExtension(ruleConfigFilePath);
        IRuleConfigParserFactory parserFactory = RuleConfigParserFactoryMap.getParserFactory(ruleConfigFileExtension);
        if (parserFactory == null) {
            throw new InvalidRuleConfigException("Rule config file format is not supported: " + ruleConfigFilePath);
        }

        IRuleConfigParser parser = parserFactory.createParser(); String configText = "";
        //从ruleConfigFilePath文件中读取配置文本到configText中
        return parser.parse(configText);
    }

    private String getFileExtension(String filePath) {
        //...解析文件名获取扩展名，比如rule.json，返回json return "json";
        return "";
    }
}
